package com.itheima.admin.controller;

import com.itheima.PageVo;
import com.itheima.Result;
import com.itheima.admin.dto.PageDto;
import com.itheima.admin.dto.RoleDto;
import com.itheima.admin.pojo.Role;
import com.itheima.admin.service.IRoleModuleService;
import com.itheima.admin.service.IRoleService;
import com.itheima.admin.vo.RoleVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController
@RequestMapping("/role")
public class  RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IRoleModuleService iRoleModuleService;
    @PostMapping("/list")
    @ApiOperation("角色条件分页查询")
    public PageVo<RoleVo> queryByPage(@RequestBody PageDto pageDto){
        return roleService.queryByPage(pageDto);
    }

    @GetMapping("/listall")
    @ApiOperation("查询所有角色")
    public List<RoleVo> queryAll(){
          return roleService.queryAll();
    }

    @PostMapping("/add")
    @ApiOperation("新增角色")
    public Result addRole(@RequestBody RoleDto roleDto){
       boolean result= roleService.addRole(roleDto);
        return new Result(result,result?"新增角色成功":"新增角色失败",null);
    }
    @ApiOperation("删除角色")
    @DeleteMapping("/{id}")
    public Result deleteRole(@PathVariable("id") String id){

        boolean result= roleService.deleteRole(id);
        return new Result(result,result?"删除角色成功":"删除角色失败",null);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询角色")
    public  RoleVo queryById(@PathVariable("id") String id){
        return  roleService.queryById(id);
    }

    @ApiOperation("修改角色")
    @PutMapping("/edit")
    public Result updateRole(@RequestBody RoleDto roleDto){
        boolean result = roleService.updateUser(roleDto);
        return new Result(result,result?"修改角色成功":"修改角色失败",null);

    }

    @ApiOperation("查询角色对应的模块")
    @GetMapping("/{id}/modules")
    public Result selectRoleModules(@PathVariable("id") String id) {
        List<Integer> data = iRoleModuleService.selectRoleModules(id);
        boolean result = data.size()!=0;
        return new Result(result,result ? "查询成功" : "查询失败", data);
    }

    @ApiOperation("角色授权")
    @PostMapping("/auth")
    public Result roleAuth(@RequestBody Map map){
       boolean result= roleService.roleAuth(map);
        return new Result(result,result?"角色授权成功":"角色授权失败",null);
    }





}
